package com.blogs.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;


public class GetAuthCodePic extends AsyncTask<String, Integer,Bitmap>{
	public interface onImageCallBack
	{
		public void onSuccess(Bitmap result);
		public void onFailed();
	}
	private onImageCallBack callback;
	public GetAuthCodePic(onImageCallBack callback) {
		// TODO Auto-generated constructor stub
		this.callback=callback;
	}
	@Override
	protected  Bitmap doInBackground(String... params) {
		// TODO Auto-generated method stub
		URL url=null;
		byte[] b=null;
		int responsecode=0;
		HttpURLConnection httpurlconnection=null;
		try {
			url = new URL("http://passport.cnblogs.com/JpegImage.aspx");
			httpurlconnection = (HttpURLConnection) url.openConnection();
			httpurlconnection.setRequestMethod("GET");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		httpurlconnection.setRequestProperty("Accept","image/png,image/*;q=0.8,*/*;q=0.5");
		httpurlconnection.setRequestProperty("Accept-Encoding","gzip,deflate");
		httpurlconnection.setRequestProperty("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
		httpurlconnection.setRequestProperty("Cache-Control","max-age=0");
		httpurlconnection.setRequestProperty("Connection","keep-alive");
		httpurlconnection.setRequestProperty("Host","passport.cnblogs.com");
		httpurlconnection.setRequestProperty("Cookie","__utma=226521935.2134156120.1367589634.1367735935.1367771505.5; __utmz=226521935.1367589634.1.1.utmcsr=passport.cnblogs.com|utmccn=(referral)|utmcmd=referral|utmcct=/login.aspx; __gads=ID=fa7de97e76fea974:T=1367589637:S=ALNI_MYHbjK2dZPjUcorYOmo30xJIQclqQ; __utmb=226521935.2.10.1367771505; __utmc=226521935; AreYouHuman="+params[0]);
		httpurlconnection.setRequestProperty("Referer","http://passport.cnblogs.com/login.aspx?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2F");
		httpurlconnection.setRequestProperty("User-Agent","Mozilla/5.0");
		httpurlconnection.setConnectTimeout(1000);
		httpurlconnection.setReadTimeout(1000);
		try {
			httpurlconnection.connect();
			responsecode = httpurlconnection.getResponseCode();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		InputStream result=null;
		if(responsecode == HttpURLConnection.HTTP_OK) //对应HTTP响应中状态行的响应码
		{
			try {
				result=httpurlconnection.getInputStream();
				
				int i = -1;
				//org.apache.commons.io.output.ByteArrayOutputStream
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((i = result.read()) != -1) {
					baos.write(i);
				}
				b=baos.toByteArray();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
		if (httpurlconnection != null) 
		{
		   httpurlconnection.disconnect();
		}
		return BitmapFactory.decodeByteArray(b, 0, b.length);
	}
	@Override
	protected void onPostExecute( Bitmap result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		if(!result.equals(""))
			this.callback.onSuccess(result);
		else
			this.callback.onFailed();	
	}
}
